import enum
import pygame

from Dialog.Dialog_For_Actor import Task1_Vice_CaptianDialog, Task1_CaptianDialog
from Dialog.Dialog_For_Systerm import TunnelTask3BeginDialog, TunnelTask3EndDialog, TunnelTaskFailDialog, \
    TunnelTask1BeginDialog, TunnelTask1EndDialog, TunnelTaskFailFoundDialog
from Task.BaseTask import BaseTask, TaskStatus


class TransmitMessageTaskStatus(enum.IntEnum):
    finding_captain = 1  # 找队长
    found_captain = 2  # 找到队长，对话中
    finding_vice_captain = 3  # 找副队长
    found_vice_captain = 4  # 副队长，对话中
    ok = 5  # 带信成功
    fail = 6  # 带信失败


class TransmitMessageTask(BaseTask):
    def __init__(self, lead_actor,japanese, captain, vice_captain, font):
        self.score = 0
        self.captain = captain
        self.vice_captain = vice_captain
        self.lead_actor = lead_actor
        self.japanese = japanese
        self.state = TransmitMessageTaskStatus.finding_captain

        self.captain_dialog = Task1_CaptianDialog(self.captain, font)
        self.vice_captain_dialog = Task1_Vice_CaptianDialog(self.vice_captain, font)
        self.task_dialog = TunnelTask1BeginDialog(font)
        self.win_dialog = TunnelTask1EndDialog(font)
        self.fail_dialog = TunnelTaskFailDialog(font)
        self.fail_found_dialog = TunnelTaskFailFoundDialog(font)
        self.time_count = 100
        super(TransmitMessageTask, self).__init__(self.task_dialog, self.win_dialog,
                                                  self.fail_dialog, self.fail_found_dialog)

    def __do_collide_actor(self, actor):
        """
        碰撞检测
        :param actor:
        :return:
        """
        temp_rect = self.lead_actor.rect
        # 调大碰撞范围
        self.lead_actor.rect = pygame.Rect(self.lead_actor.pos_x + 5,
                                           self.lead_actor.pos_y + 5, 20, 60)
        is_collide = pygame.sprite.collide_rect(self.lead_actor, actor)
        self.lead_actor.rect = temp_rect
        if is_collide:
            return True

    def draw_task(self, current_surface, x, y):
        """
        绘制人物和对话框
        :param current_surface: 
        :param x: 
        :param y: 
        :return: 
        """
        self.captain.draw(current_surface, x, y)
        if self.state == TransmitMessageTaskStatus.found_captain:
            self.captain_dialog.draw(current_surface, x, y)
            if self.captain_dialog.is_finish():
                self.state = TransmitMessageTaskStatus.finding_vice_captain

        elif self.state == TransmitMessageTaskStatus.found_vice_captain:
            self.vice_captain_dialog.draw(current_surface, x, y)
            if self.vice_captain_dialog.is_finish():
                self.state = TransmitMessageTaskStatus.ok
                self.task_status = TaskStatus.win_dialog
        if self.state >= TransmitMessageTaskStatus.finding_vice_captain:
            self.vice_captain.draw_for_invisible(current_surface, x, y)

    def do_self_task(self, key_down, key_list):
        """
        任务的执行
        :param key_down:
        :param key_list:
        :return:
        """
        # 找队长
        if self.state == TransmitMessageTaskStatus.finding_captain:
            # 找到队长
            if self.__do_collide_actor(self.captain):
                self.state = TransmitMessageTaskStatus.found_captain
        # 找副队长
        elif self.state == TransmitMessageTaskStatus.finding_vice_captain:
            # 找到副队长
            if self.__do_collide_actor(self.vice_captain):
                self.state = TransmitMessageTaskStatus.found_vice_captain
        # 找到队长后按键处理字幕
        elif self.state == TransmitMessageTaskStatus.found_captain:
            if key_down and key_list[pygame.K_SPACE]:
                self.captain_dialog.next_text()
            if self.captain_dialog.is_finish():
                self.state = TransmitMessageTaskStatus.finding_vice_captain
        # 找到副队长后按键处理字幕
        elif self.state == TransmitMessageTaskStatus.found_vice_captain:
            if key_down and key_list[pygame.K_SPACE]:
                self.vice_captain_dialog.next_text()
            if self.vice_captain_dialog.is_finish():
                self.state = TransmitMessageTaskStatus.ok
                self.task_status = TaskStatus.win_dialog

        # 是否被发现
        list_collide = pygame.sprite.spritecollide(self.lead_actor, self.japanese, False)
        if len(list_collide) > 0:
            self.task_status = TaskStatus.fail_found_dialog
